﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using KPIS.GERP.SERVICE.DAL;
using KPIS.GERP.SERVICE.Model;

namespace KPIS.GERP.SERVICE.WorkflowService.WorkflowService
{
    [ServiceContract]
    public interface IAbsenceApprovalWCF
    {
        [OperationContract]
        List<approval_task> GetTasks();

        [OperationContract]
        List<approval_task> GetTasksDb();

    }
    
    public class AbsenceApprovalWCF : IAbsenceApprovalWCF
    {
        #region Retreive Task

        public List<approval_task> AllTask()
        {
            if (ApprovalStorage.Tasks == null)
                ApprovalStorage.Tasks = new Dictionary<Guid, approval_task>();

            var approvalTasksListInMemory = ApprovalStorage.Tasks.Values.ToList();
            var approvalTaskListInDatabase = new ApprovalTaskDal().GetApprovalTask(new approval_task(), true, false);
            var approvalTaskListResult = new List<approval_task>();

            foreach (var atDb in approvalTaskListInDatabase)
            {
                // Check is in memory also
                bool atDbIsExistInMemory = approvalTasksListInMemory.FirstOrDefault(o => o.ApprovalTaskSeq == atDb.ApprovalTaskSeq) != null;

                // If found in memory, add 
                if(atDbIsExistInMemory)
                    approvalTaskListResult.Add(approvalTasksListInMemory.FirstOrDefault(o => o.ApprovalTaskSeq == atDb.ApprovalTaskSeq));
                else
                {
                    approvalTaskListResult.Add(atDb);
                }
            }

            return approvalTaskListResult;
        }

        public List<approval_task> GetTasks()
        {
            return AllTask();
        } 

        public List<approval_task> GetTasksMyRequest(int empSeq)
        {
            return (from t in AllTask()
                    where t.RequestEmpSeq == empSeq
                    select t).ToList();
        }

        public List<approval_task> GetTasksMyRequestInProcess(int empSeq)
        {
            return (from t in AllTask()
                    where t.RequestEmpSeq == empSeq && !t.Completed
                    select t).ToList();
        }

        public List<approval_task> GetTaskMyWaitingApprove(int empSeq)
        {
            return (from t in AllTask()
                    where t.CurrentRuleApprove != null && t.CurrentRuleApprove.Approvers != null &&
                    (from a in t.CurrentRuleApprove.Approvers select a.emp_seq).ToList().Contains(empSeq)
                    select t).ToList();
        } 

        public List<approval_task> GetTasksDb()
        {
            List<approval_task> result = new ApprovalTaskDal().GetApprovalTask(new approval_task(), true);

            ApprovalStorage.Tasks = new Dictionary<Guid, approval_task>();
            foreach (var r in result)
            {
                try
                {
                    ApprovalStorage.Tasks.Add(r.WfInstantId, r);
                }
                catch { }
            }

            return result;
        }

        #endregion 
    }
}
